package com.red.wood.module.eval.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.springjdbc.CommonDao;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.IMap;
import com.red.wood.module.eval.dao.ApplicationCdnDao;

@Repository
public class ApplicationCdnDaoImpl implements ApplicationCdnDao{

	@Autowired
	private CommonDao dao;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public void add(Map<String, Object> paramMap) {
		StringBuilder sqlSb = new StringBuilder();
		sqlSb.append("insert into t_eval_application_cdn (");
		dao.insertTableData(paramMap, sqlSb);
	}

	@Override
	public Map<String, Object> getEntity(Map<String, Object> paramMap) {
		String singleTaskId = CommonUtils.valueOf(paramMap.get("singleTaskId"));
		if(StringUtils.isNotBlank(singleTaskId)){
			String sql = "select id as id, singleTaskId as singleTaskId, number as number, applicationDate as applicationDate, name as name, address as address, code as code, contacts as contacts, tel as tel, moveTle as moveTle, fax as fax, eMail as eMail, type as type, gbps as gbps, linkNum as linkNum, systemName as systemName, productionUnit as productionUnit, version as version, handleInfo as handleInfo, remark as remark, duties as duties, pushTime as pushTime,houseNum as houseNum,houseName as houseName,isShare as isShare,gameProvince as gameProvince, gameCity as gameCity, houseAddress as houseAddress, postalCode as postalCode, idcType as idcType, unitFullName as unitFullName, contractName as contractName, signTime as signTime, contractEffBeginTime as contractEffBeginTime, contractEffEndTime as contractEffEndTime, moduleType1 as moduleType1, moduleContent1 as moduleContent1, moduleType2 as moduleType2, moduleContent2 as moduleContent2, houseNodeName as houseNodeName, nodeNum as nodeNum, evalType as evalType, lastEvalTime as lastEvalTime, lastNum as lastNum, lastMechanism as lastMechanism, deployInfo as deployInfo,deployIsShare as deployIsShare, deployProvince as deployProvince,deployCity as deployCity, deployAddr as deployAddr,deployCode as deployCode, deployHouseName as deployHouseName,registerProvince,registerCity from t_eval_application_cdn where singleTaskId=" + "'"+singleTaskId+"'";
			List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
			if(null != list && !list.isEmpty()){
				Map<String,Object> applicationCdn  = list.get(0);
				if(applicationCdn != null){
					if(applicationCdn.get("applicationDate") != null){
						Date lastSubmitTime = (Date)applicationCdn.get("applicationDate");
						SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						applicationCdn.put("applicationDate", formatter.format(lastSubmitTime));
					}
					if(applicationCdn.get("pushTime") != null){
						Date lastSubmitTime = (Date)applicationCdn.get("pushTime");
						SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						applicationCdn.put("pushTime", formatter.format(lastSubmitTime));
					}
					if(applicationCdn.get("signTime") != null){
						/*Date signTime = (Date)applicationCdn.get("signTime");
						SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						applicationCdn.put("signTime", formatter.format(signTime));*/
						applicationCdn.put("signTime",applicationCdn.get("signTime"));
					}
					if(applicationCdn.get("contractEffBeginTime") != null){
						/*Date contractEffBeginTime = (Date)applicationCdn.get("contractEffBeginTime");
						SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						applicationCdn.put("contractEffBeginTime", formatter.format(contractEffBeginTime));*/
						applicationCdn.put("contractEffBeginTime",applicationCdn.get("contractEffBeginTime"));
					}
					if(applicationCdn.get("contractEffEndTime") != null){
						/*Date contractEffEndTime = (Date)applicationCdn.get("contractEffEndTime");
						SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						applicationCdn.put("contractEffEndTime", formatter.format(contractEffEndTime));*/
						applicationCdn.put("contractEffEndTime",applicationCdn.get("contractEffEndTime"));
					}
					if(applicationCdn.get("lastEvalTime") != null){
						Date lastEvalTime = (Date)applicationCdn.get("lastEvalTime");
						SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						applicationCdn.put("lastEvalTime", formatter.format(lastEvalTime));
					}
				}
				return applicationCdn;
			}
		}
		return null;
	}
	
	public void update(IMap paramMap){
		String singleTaskId = CommonUtils.valueOf(paramMap.get("singleTaskId"));
		if(StringUtils.isNotBlank(singleTaskId)){
			StringBuilder sqlSb = new StringBuilder();
			sqlSb.append("update t_eval_application_cdn set ");
			String queryConf = " where singleTaskId=" + singleTaskId;
			dao.updateTableData(paramMap, sqlSb, queryConf);
		}
	}
	
	public long getBySingleTaskId(String singleTaskId){
		if(StringUtils.isNotBlank(singleTaskId)){
			String sql = "select count(1) as count from t_eval_application_cdn where singleTaskId = " + singleTaskId;
			List<Map<String, Object>> dataList = jdbcTemplate.queryForList(sql);
			if(dataList != null && !dataList.isEmpty()){
				return Long.valueOf(dataList.get(0).get("count").toString());
			}
		}
		return 0;
	} 
	
	public Map<String, Object> getInfoBySingleTaskId(String singleTaskId){
		if(StringUtils.isNotBlank(singleTaskId)){
			String sql = "select id as id, singleTaskId as singleTaskId, number as number, applicationDate as applicationDate, name as name, address as address, code as code, contacts as contacts, tel as tel, moveTle as moveTle, fax as fax, eMail as eMail, type as type, gbps as gbps, linkNum as linkNum, systemName as systemName, productionUnit as productionUnit, version as version, handleInfo as handleInfo, remark as remark, duties as duties, pushTime as pushTime,houseNum as houseNum,houseName as houseName,isShare as isShare,gameProvince as gameProvince, gameCity as gameCity, houseAddress as houseAddress, postalCode as postalCode, idcType as idcType, unitFullName as unitFullName, contractName as contractName, signTime as signTime, contractEffBeginTime as contractEffBeginTime, contractEffEndTime as contractEffEndTime, moduleType1 as moduleType1, moduleContent1 as moduleContent1, moduleType2 as moduleType2, moduleContent2 as moduleContent2, houseNodeName as houseNodeName, nodeNum as nodeNum, evalType as evalType, lastEvalTime as lastEvalTime, lastNum as lastNum, lastMechanism as lastMechanism, deployInfo as deployInfo,deployIsShare as deployIsShare, deployProvince as deployProvince,deployCity as deployCity, deployAddr as deployAddr,deployCode as deployCode, deployHouseName as deployHouseName from t_eval_application_cdn where singleTaskId=" + "'"+singleTaskId+"'";
			List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
			if(null != list && !list.isEmpty()){
				Map<String,Object> applicationCdn  = list.get(0);
				return applicationCdn;
			}
		}
		return null;
	}
	
	public long getMaxId(){
		String sql = "select max(id) as id from t_eval_application_cdn";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		if(null != list && !list.isEmpty() && list.get(0).get("id") != null){
			return Long.valueOf(list.get(0).get("id").toString());
		}
		return 0;
	}
	
	public Map<String, Object> getLastData(){
		String sql = "select * from t_eval_application_cdn order by id desc limit 1";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		if(null != list && !list.isEmpty() && list.get(0).get("id") != null){
			return list.get(0);
		}
		return null;
	}
}
